//
//  ZCMeRaiseVC.m
//  zctx
//
//  Created by apple on 15/10/13.
//  Copyright © 2015年 sanliang. All rights reserved.
//

#import "ZCMeRaiseVC.h"
#import "ZCmainCell.h"

#import "ZCMeAuditDetailsVC.h"
#import "ZCInvolvedModel.h"

#import "ZCmainCell.h"
#import "ZCProjectdetailsVC.h"


#import "ZCSamplingOKCell.h"
#import "ZCMeRaiseCell.h"
#import "ZCInvolvedDetailsVC.h"

#import "ZCSamplingDetailVC.h"
#import "ZCSamplingCell.h"

@interface ZCMeRaiseVC ()
@property BOOL  isnothing;
@end

@implementation ZCMeRaiseVC

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [MobClick beginLogPageView:@"ZCMeRaiseVC"];
    [MTA trackPageViewBegin:@"ZCMeRaiseVC"];
    self.tabBarController.hidesBottomBarWhenPushed = YES;
}
- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    [MobClick endLogPageView:@"ZCMeRaiseVC"];
    [MTA trackPageViewEnd:@"ZCMeRaiseVC"];
    self.tabBarController.hidesBottomBarWhenPushed = NO;
}

-(void)updateRequest{
    if ([AFNetworkReachabilityManager sharedManager].networkReachabilityStatus >0) {
        self.bannerView.hidden = YES;
    }
    [self requestSeeResultdata];
}

- (void)viewDidLoad {
    [super viewDidLoad];
//    self.edgesForExtendedLayout = UIRectEdgeNone;
    //AF 开启监听网络状态
    [[AFNetworkReachabilityManager sharedManager] startMonitoring];
    self.navigationItem.title = @"参与的项目";
    [self initnavegation];
    self.isnothing = NO;
    dataArray = [[NSMutableArray alloc]init];
    
    [self initTableView];
    [self requestSeeResultdata]; //请求数据
    
    [self.tableView registerNib:[UINib nibWithNibName:@"ZCmainCell" bundle:nil] forCellReuseIdentifier:@"maincell"];
    [self.tableView registerNib:[UINib nibWithNibName:@"ZCSamplingCell" bundle:nil] forCellReuseIdentifier:@"samcell"];
    [self.tableView registerNib:[UINib nibWithNibName:@"ZCMeRaiseCell" bundle:nil] forCellReuseIdentifier:@"meraisecell"];
    
    [self.tableView registerNib:[UINib nibWithNibName:@"ZCSamplingOKCell" bundle:nil] forCellReuseIdentifier:@"samOKcell"];
    
//    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
//        // 进入刷新状态后会自动调用这个block
//        [self requestSeeResultdata];
//    }];
//    [self.tableView.mj_header beginRefreshing];
}

#pragma mark 导航栏
-(void)initnavegation{
    
    UIButton * rightButton = [UIButton buttonWithType:UIButtonTypeCustom];
    rightButton.frame = CGRectMake(0, 0, 26, 25);
    [rightButton setImage:[UIImage imageNamed:@"navigation_search"] forState:UIControlStateNormal];
    [rightButton addTarget:self action:@selector(searchButtonPressed) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem * barButtonItem_rightNavBar = [[UIBarButtonItem alloc] initWithCustomView:rightButton];
    self.navigationItem.rightBarButtonItem = barButtonItem_rightNavBar;
    
    UIButton * leftButton = [UIButton buttonWithType:UIButtonTypeCustom];
    leftButton.frame = CGRectMake(0, 0, 19, 36);
    [leftButton setImage:[UIImage imageNamed:@"navigation_back"] forState:UIControlStateNormal];
    [leftButton addTarget:self action:@selector(backButtonPressed) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem * barButtonItem_leftNavBar = [[UIBarButtonItem alloc] initWithCustomView:leftButton];
    self.navigationItem.leftBarButtonItem = barButtonItem_leftNavBar;
}

-(void)backButtonPressed{
 
    [self.navigationController popViewControllerAnimated:YES];
}

#pragma mark 请求结果数据
-(void)requestSeeResultdata{
    [GMDCircleLoader setOnView:self.view withTitle:@"努力加载中..." animated:YES];
    [NetworkManager requestGetMyOrdersWithMid:UserModel.mid password:UserModel.password success:^(NSDictionary *ArrayResponse) {
        [GMDCircleLoader hideFromView:self.view animated:YES];
        if ([[ArrayResponse objectForKey:@"content"] isKindOfClass:[NSArray class]]) {
            NSArray *array = (NSArray *)[ArrayResponse objectForKey:@"content"];
            if (array.count>0) {
                self.isnothing = NO;
                dataArray = [array mutableCopy];
                [self.tableView reloadData];
            }else{
                self.isnothing = YES;
            }
        }else{
            self.isnothing = YES;
            [self.tableView reloadData];
        }
    
        } failure:^(NSError *error) {
            [GMDCircleLoader hideFromView:self.view animated:YES];
            if ([AFNetworkReachabilityManager sharedManager].networkReachabilityStatus == 0) {
                self.bannerView.hidden = NO;
            }
    }];
}

-(void)initTableView{
    self.tableView = [[UITableView alloc]initWithFrame:CGRectZero style:UITableViewStyleGrouped];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.backgroundColor = [UIColor colorWithHexString:@"#fafcfb"];
    [self.view addSubview:self.tableView];
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.view).with.insets(UIEdgeInsetsMake(0, 0,0,0));
    }];
}

#pragma mark UITableViewDataSource,UITableViewDelegate

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return  dataArray.count;
    
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    ZCInvolvedModel *moel =[ZCInvolvedModel modelObjectWithDictionary:dataArray[indexPath.row]];
    NSString *resultStr = [NSString stringWithFormat:@"%@",moel.result];
    NSString *stateStr = [NSString stringWithFormat:@"%@",moel.state];
    
    if (([resultStr isEqualToString:@"0"] && [stateStr isEqualToString:@"1"])||([resultStr isEqualToString:@"0"] && [stateStr isEqualToString:@"0"])) {//众筹中和审核中
            static NSString *cellid =@"meraisecell";
            ZCMeRaiseCell *cell = [tableView dequeueReusableCellWithIdentifier:cellid];
            if (cell == nil) {
                cell = [[ZCMeRaiseCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellid];
            }
            cell.mainModel = moel;
            return cell;
    }else if (![resultStr isEqualToString:@"0"]){//合格或不合格
        
        if ([resultStr isEqualToString:@"2"]) {
            static NSString *cellid =@"samcell";
            ZCSamplingCell *cell = [tableView dequeueReusableCellWithIdentifier:cellid];
            if (cell == nil) {
                cell = [[ZCSamplingCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellid];
            }
            cell.samplingModel = moel;
            return cell;
            
        }else{
            static NSString *cellid =@"samOKcell";
            ZCSamplingOKCell *cell = [tableView dequeueReusableCellWithIdentifier:cellid];
            if (cell == nil) {
                cell = [[ZCSamplingOKCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellid];
            }
            cell.samplingOKModel = moel;
            return cell;
            
        }

//        static NSString *cellid =@"samcell";
//        ZCSamplingCell *cell = [tableView dequeueReusableCellWithIdentifier:cellid];
//        if (cell == nil) {
//            cell = [[ZCSamplingCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellid];
//        }
//        cell.samplingModel = moel;
//        return cell;
    }
    else
    {// 0 审核中  2 抽样中 3完成 4 异常
        static NSString *cellid =@"maincell";
        ZCmainCell *cell = [tableView dequeueReusableCellWithIdentifier:cellid];
        if (cell == nil) {
            cell = [[ZCmainCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellid];
        }
        cell.mainModel = moel;
        return cell;
    }
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 90;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

    [self.tableView deselectRowAtIndexPath:indexPath animated:YES];
    ZCInvolvedModel *moel =[ZCInvolvedModel modelObjectWithDictionary:dataArray[indexPath.row]];
    NSString *mid =[NSString stringWithFormat:@"%@",moel.internalBaseClassIdentifier];
    NSString *resultStr = [NSString stringWithFormat:@"%@",moel.result];
    NSString *stateStr = [NSString stringWithFormat:@"%@",moel.state];

    NSLog(@"result = %@,state = %@",resultStr,stateStr);
    if ([resultStr isEqualToString:@"0"]) {
        if ([stateStr isEqualToString:@"0"]) {
            ZCInvolvedDetailsVC *detailVC= [[ZCInvolvedDetailsVC alloc]init];
            detailVC.pid = mid;
            detailVC.stateid = @"0";
            detailVC.hidesBottomBarWhenPushed = YES;
            [self.navigationController pushViewController:detailVC animated:YES];
        }
        else if([stateStr isEqualToString:@"1"])
        {
            ZCInvolvedDetailsVC *detailVC = [[ZCInvolvedDetailsVC alloc]init];
            detailVC.pid = mid;
            detailVC.stateid = @"1";
            detailVC.hidesBottomBarWhenPushed = YES;
            [self.navigationController pushViewController:detailVC animated:YES];
        }
        else if([stateStr isEqualToString:@"2"])
        {
            ZCInvolvedDetailsVC *detailVC = [[ZCInvolvedDetailsVC alloc]init];
            detailVC.pid = mid;
            detailVC.hidesBottomBarWhenPushed = YES;
            [self.navigationController pushViewController:detailVC animated:YES];
        }
    }else{
        ZCInvolvedDetailsVC *detailVC = [[ZCInvolvedDetailsVC alloc]init];
        detailVC.pid = mid;
        detailVC.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:detailVC animated:YES];
    }
}


-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 0.0001;
}

-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    
    if (self.isnothing ==NO) {
        UIView *footView = [[UIView alloc]init];
        footView.backgroundColor = [UIColor colorWithHexString:@"#fafcfb"];
        return footView;
    }else{
        
        UIView *footView = [[UIView alloc]initWithFrame:CGRectMake(0, 0,SCREEN_WIDTH,400)];
        footView.backgroundColor = [UIColor colorWithHexString:@"#fafcfb"];
        
        UIImageView*redImg = [[UIImageView alloc]init];
        [footView addSubview:redImg];
        [redImg makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(footView.top).with.offset(0.255 * SCREEN_HEIGHT);
            make.centerX.equalTo (footView.centerX).with.offset(0);
            make.height.mas_equalTo(100);
            make.width.mas_equalTo(100);
        }];
        [redImg setImage:[UIImage imageNamed:@"me_raise_no"]];
        
        UILabel*redLable = [[UILabel alloc]init];
        [footView addSubview:redLable];
        [redLable makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(redImg.bottom).with.offset(23);
            make.centerX.equalTo (footView.centerX).with.offset(0);
            make.height.mas_equalTo(21);
            make.width.mas_equalTo(150);
        }];
        redLable.textAlignment = NSTextAlignmentCenter;
        redLable.text = @"没有参与的项目";
        redLable.textColor = [UIColor colorWithHexString:@"#d2d2d2"];
        redLable.font = [UIFont systemFontOfSize:16];
        return footView;
    }
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    if (self.isnothing ==NO) {
        return 0.001;
    }else{
        return 400;
    }
}

#pragma mark 搜索
-(void)searchButtonPressed{
    
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
